package com.lantosec.hmrp.modules.morality.controller;


import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.log.ControllerLog;
import com.lantosec.hmrp.core.log.LogType;
import com.lantosec.hmrp.core.util.DateUtils;
import com.lantosec.hmrp.modules.morality.entity.MoralityBranchDept;
import com.lantosec.hmrp.modules.morality.entity.MoralityPlanList;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.modules.morality.entity.MoralityPlan;
import com.lantosec.hmrp.modules.morality.service.IMoralityPlanService;
import com.lantosec.hmrp.modules.morality.vo.MoralityPlanVO;

import java.util.Arrays;
import java.util.List;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.RestController;
import com.lantosec.hmrp.core.mvc.BaseController;

/**
 *
 * 考评方案 控制器
 *
 * @author AutoCode
 * @since 2020-04-16
 */
@Api(value = "考评方案控制器", tags = "考评方案控制器")
@RestController
@RequestMapping("/morality/plan")
@Log4j2
public class MoralityPlanController extends BaseController {
    @Autowired
    private IMoralityPlanService service;

    @GetMapping("all")
    @ApiOperation(value = "查询全部")
    public Result<List<MoralityPlanVO>> all(){
        return Result.ok(entity2VO(service.list()));
    }

    @GetMapping("page")
    @ApiOperation(value = "分页查询")
    public Result<PageVO<MoralityPlanVO>> page(@ApiParam(value = "分页对象", required = true) PageVO<MoralityPlanVO> page,
                                             @ApiParam(value = "考评方案名称") @RequestParam(name = "moralityPlanName",required = false) String moralityPlanName,
                                             @ApiParam(value = "周期名称") @RequestParam(name = "moralityCycleId",required = false) Integer moralityCycleId){
        service.getAll(page,moralityPlanName,moralityCycleId);
        return Result.ok(page);
    }

    @GetMapping("get/{id}")
    @ApiOperation(value = "主键查询")
    public Result<MoralityPlanVO> get(@ApiParam(value = "ID", required = true) @PathVariable(name="id") String id){
        return Result.ok(entity2VO(service.getById(id)));
    }

    @PostMapping("create")
    @ControllerLog(name = "新增考评方案", type = LogType.add)
    @ApiOperation(value = "新增数据")
    public Result<Integer> create(@ApiParam(value = "考评方案VO对象", required = true) @RequestBody @Validated MoralityPlanList moralityPlanList){
        Integer moralityPlanId = service.saveMoralityPlan(moralityPlanList);
        return Result.ok(moralityPlanId);
    }

    @PostMapping("saveBranchDept")
    @ControllerLog(name = "新增党支部", type = LogType.add)
    @ApiOperation(value = "新增党支部")
    public Result<Integer> saveBranchDept(@ApiParam(value = "党支部部门挂接表", required = true) @RequestBody List<MoralityBranchDept> moralityBranchDept){
        service.saveBranchDept(moralityBranchDept);
        return Result.ok();
    }

    @GetMapping("getBranchOfDeptId")
    @ApiOperation(value = "查询系统中党支部已设定的科室")
    public Result<List<String>> getBranchOfDeptId(){
        List<String> moralityBranchDepts = service.getBranchOfDeptId();
        return Result.ok(moralityBranchDepts);
    }

    @GetMapping("getBranchDept")
    @ApiOperation(value = "查询党支部")
    public Result<List<MoralityBranchDept>> getBranchDept(@ApiParam(value = "分页对象", required = true) PageVO<MoralityBranchDept> page,
                                                @ApiParam(value = "党支部ID") @RequestParam(name="branchId",required = false) String branchId,
                                                @ApiParam(value = "部门ID") @RequestParam(name="deptId",required = false) String deptId){
        List<MoralityBranchDept> moralityBranchDepts = service.getBranchDept(page,branchId,deptId);
        return Result.ok(moralityBranchDepts);
    }

    @PutMapping("updateBranchDept")
    @ControllerLog(name = "修改党支部", type = LogType.update)
    @ApiOperation(value = "修改党支部")
    public Result<Void> updateBranchDept(@ApiParam(value = "党支部部门挂接表", required = true) @RequestBody MoralityBranchDept moralityBranchDept){
        service.updateBranchDept(moralityBranchDept);
        return Result.ok();
    }

    @DeleteMapping(value = "deleteBranchDept/{branchId}")
    @ControllerLog(name = "删除党支部", type = LogType.delete)
    @ApiOperation("删除党支部")
    public Result<Void> deleteBranchDept(@ApiParam(value = "党支部ID", required = true) @PathVariable(name="branchId") String branchId){
        service.deleteBranchDept(branchId);
        return Result.ok();
    }

    @PutMapping("update")
    @ControllerLog(name = "修改考评方案", type = LogType.update)
    @ApiOperation(value = "修改数据")
    public Result<Void> update(@ApiParam(value = "考评方案VO对象", required = true) @RequestBody MoralityPlanList moralityPlanList){
        service.updateMoralityPlan(moralityPlanList);
        return Result.ok();
    }

    @DeleteMapping(value = "delete/{moralityPlanId}")
    @ControllerLog(name = "逻辑删除考评方案", type = LogType.delete)
    @ApiOperation("逻辑删除")
    public Result<Void> delete(@ApiParam(value = "moralityPlanId", required = true) @PathVariable(name="moralityPlanId") Integer moralityPlanId){
        service.deleteMoralityPlan(moralityPlanId);
        return Result.ok();
    }
}
